package first.endtoend.webservices.gcm

import first.endtoend.tsm.AgentMobilePhone

/**
 * Exemple : https://github.com/osoco/grails-android-gcm-server-example/blob/master/grails-app/controllers/es/osoco/android/gcm/AndroidGcmSenderController.groovy
 */
class AndroidGCMSenderController
{

    /**
     * A plugin that provides a frontend for accesing Google Cloud Messaging service for Android. The plugin provides a service (androidGcmService) that has the following methods:

     sendCollapseMessage - sends a collapse message to one device
     sendMulticastCollapseMessage - sends a collapse message to multiple devices
     sendInstantMessage - sends an instant message to one device
     sendMulticastInstantMessage - sends and instant message to multiple devices
     sendMessage - sends a message (collapsed if a collapse key is provided) to the given devices
     The plugin can be configured with the following options (to be placed in the project Config.groovy):

     android.gcm.api.key - The GCM service API key. Must be passed as a method parameter if is not provided in the config
     android.gcm.time.to.live - The message TTL (2419200 by default)
     android.gcm.delay.'while'.idle - Should the message be delayed with the device is idle (false by default)
     android.gcm.retries - Number of message sent retries (1 by default)
     */
    def androidGcmService
    def grailsApplication
    def beforeInterceptor = {
        if (params.projectId)
        {
            params.tokens = (AgentMobilePhone.findAllByProjectId(params.projectId) +
                    AgentMobilePhone.findAllByProjectIdIsNull())*.token
        }
    }
    def configureMessage = {
        params.apiKey = grailsApplication.config.android.gcm.api.key ?: ''
        render view: 'index', model: params
    }
    def index = {
        render view: 'index', model: params
    }
    def sendMessage = {
        ['deviceToken', 'messageKey', 'messageValue'].each {
            key -> params[key] = [params[key]].flatten().findAll { it }
        }
        def messages = params.messageKey.inject([:]) {
            currentMessages, currentKey ->
                currentMessages << [(currentKey): params.messageValue[currentMessages.size()]]
        }
        flash.message = 'received.message.response'
        flash.args = [androidGcmService.sendMessage(messages, params.deviceToken,
                params.collapseKey, params.apiKey).toString()]
        redirect(action: 'index', params: params)
    }
}
